﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using FinPlusInterfaces;
using FinPlusCompCore;

namespace FinPlusCompProxy
{
    public class SwapRate : IRate
    {
        public double Value { get { return _level.Value; } }
        public int Shock { get; set; }
        public string Instrument { get { return _instrumentName; } }

        private enum Params { ShockUnit, Maturity, SettlementDays, Index, FixLegFrq, FixLegDayCount, FixLegConv, Holidays };
        private FinPlusLevel _level;
        private QLAnalytics _analytics;
        private IPod _instrumentBuild, _instrument;
        private string _instrumentName, _tenor, _floatLegIndex, _fixedLegFreq, _fixedLegDayCount, _fixedLegBizConv, _holidays;
        private int _settlementDays;
        private double _shockUnit;

        public SwapRate(QLAnalytics analytics, string instrumentName, IPod instrument, IPod instrumentBuild)
        {
            _analytics = analytics;
            _instrumentName = instrumentName;
            _instrument = instrument;
            _instrumentBuild = instrumentBuild;

            _level = new FinPlusLevel(analytics.BuySell);
            _tenor = _instrumentBuild[Params.Maturity.ToString()].ToString();
            _settlementDays = (int)_instrumentBuild.Value(Params.SettlementDays.ToString());
            _floatLegIndex = _instrumentBuild[Params.Index.ToString()].ToString();
            _fixedLegFreq = _instrumentBuild[Params.FixLegFrq.ToString()].ToString();
            _fixedLegDayCount = _instrumentBuild[Params.FixLegDayCount.ToString()].ToString();
            _fixedLegBizConv = _instrumentBuild[Params.FixLegConv.ToString()].ToString();
            _holidays = _instrumentBuild[Params.Holidays.ToString()].ToString();
            _shockUnit = (double)_instrument.Get(Params.ShockUnit.ToString()).Object;
        }

        public string Build(string buySell)
        {
            lock (_analytics.QL)
                return _analytics.QL.SwapRate(_analytics.MarketName, _instrumentName, _level.Value + _shockUnit * Shock, _tenor, _settlementDays, 0, _floatLegIndex, _fixedLegFreq, _fixedLegDayCount, _fixedLegBizConv, _holidays);
        }

        public bool Update(IPod level)
        {
            return _level.LevelUpdate(level);
        }
    }
}
